layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;

	//添加验证规则
	form.verify({
		newPwd: function (value, item) {
			if (value.length < 6) {
				return "密码长度不能小于6位";
			}
		},
		confirmPwd: function (value, item) {
			if (!new RegExp($("#newPwd").val()).test(value)) {
				return "两次输入密码不一致，请重新输入！";
			}
		}
	})
	//提交
	form.on("submit(changePwd)", function (data) {
		var index = layer.msg('提交中，请稍候', {
			icon: 16,
			time: false,
			shade: 0.8
		});
		var newPwd = $('#newPwd').val();
		var confirmPwd = $('#confirmPwd').val();
		var admin = localStorage.getItem("ChoiceID")
		console.log(data)
		if ($.md5(newPwd) != $.md5(confirmPwd)) {
			layer.alert('两次密码不一样请重新输入', { 'icon': 2 });
			console.log(newPwd)
			console.log(confirmPwd)
			return;
		}


		$.ajax({
			url: "http://127.0.0.1:8000/pswchange",
			type: "post",
			dataType: "JSON",
			data: { "username": admin, "password": newPwd },
			success: function (res) {
				if (res.code > 0) {

					setTimeout(function () {
						layer.close(index);
						layer.msg(res.msg);
					}, 2000);
				}
				if (res.code == 0) {
					setTimeout(function () {
						layer.close(index);
						layer.msg(res.msg);
					}, 2000);
				}
			}
		})

		return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});

})